package com.idbk.chargestation.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.idbk.chargestation.fragment.FragmentComments;
import com.idbk.chargestation.fragment.FragmentPiles;
import com.idbk.chargestation.fragment.FragmentPointDetail;
import com.idbk.chargestation.util.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 桩详情fragment 适配器
 * Created by zhx on 2017/5/8.
 */

public class PointDetailAdapter extends FragmentPagerAdapter {

    private List<Fragment> mListFragment;

    private final String[] mTitles;

    public PointDetailAdapter(FragmentManager fm, int pointId, int pileCount, int commentCount) {
        super(fm);
        mListFragment = new ArrayList<Fragment>();
        FragmentPointDetail detail = new FragmentPointDetail();
        mListFragment.add(detail);

        Bundle bundle1 = new Bundle();
        bundle1.putInt(Const.KEY_ID, pointId);
        FragmentPiles piles = new FragmentPiles();
        piles.setArguments(bundle1);
        mListFragment.add(piles);

        Bundle bundle2 = new Bundle();
        bundle2.putInt(Const.KEY_ID, pointId);
        FragmentComments comments = new FragmentComments();
        comments.setArguments(bundle2);
        mListFragment.add(comments);

        mTitles = new String[]{
                "详情",
                getCompactNumberTitle("电桩", pileCount),
                getCompactNumberTitle("评论", commentCount)};
    }

    public static String getCompactNumberTitle(String t, int count) {
        if (count < 99) {
            return String.format(Locale.CHINA, "%s(%d)", t, count);
        } else {
            return String.format(Locale.CHINA, "%s(99+)", t);
        }
    }

    public static String getCompactNumberTitle(String t, int total, int idleCount) {
        if (total < 99) {
            return String.format(Locale.CHINA, "%s(%d/%d)", t, idleCount, total);
        } else {
            return String.format(Locale.CHINA, "%s(99+)", t);
        }
    }

    @Override
    public Fragment getItem(int arg0) {
        return mListFragment.get(arg0);
    }

    @Override
    public int getCount() {
        return mListFragment.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

}
